axios.defaults.baseURL = 'http://localhost:3000'

// 添加请求拦截器
axios.interceptors.request.use((config) => {
  const token = localStorage.getItem('token')
  if (token) {
    // 如果 token 存在，在每个 HTTP header 都加上 token
    // Bearer 是 JWT 的认证头部信息
    config.headers.common['Authorization'] = `Bearer ${token}`
  }
  return config
})

// 注册
export const register = async ({ username, password }) => {
  const res = await axios.post('/api/register', {
    username,
    password,
    type: 'common',
  })
  console.log(res.data)
  return res.data
}
// 新增全类型用户
export const registerAll = async ({ username, password, type }) => {
  const res = await axios.post('/api/register', {
    username,
    password,
    type,
  })
  console.log(res.data)
  // return res.data
}
//登录
export const login = async ({ username, password, type }) => {
  const res = await axios.post('/api/login', {
    username,
    password,
    type,
  })
  console.log(res.data)
  //将token设置到localStorage中
  localStorage.setItem('token', res.data.token)
  return res.data
}
//退出登录
export const logout = async () => {
  const username = localStorage.getItem('username')
  const type = localStorage.getItem('type')
  let res = await axios.post('/api/logout', {
    username,
    type,
  })
  console.log(res.data)
  localStorage.removeItem('token') //删除token
  localStorage.removeItem('username') //删除username
  localStorage.removeItem('path') //删除path
  localStorage.removeItem('type') //删除type
}
//下线
export const offline = async ({username, type}) => {
  let res = await axios.post('/api/logout', {
    username,
    type,
  })
  console.log(res.data)
}
//获取用户信息
export const getUser = async () => {
  try {
    const res = await axios.get('/api/user')
    return res.data
  } catch (err) {
    console.log(err)
  }
}
//删除用户
export const deleteUser = async ({ username, type }) => {
  const res = await axios.post('/api/delete', {
    username,
    type
  })
  // console.log(res.data)
}
//获取公告信息
export const getReport = async () => {
  try {
    var res = await axios.get('/api/notice?type=report')
    console.log(res.data)
    return res.data
  } catch (err) {
    console.log(err)
  }
}
//发布公告
export const createNotice = async ({
  title,
  content,
  publisher,
  area,
  time,
}) => {
  const res = await axios.post('/api/report', {
    title,
    content,
    publisher,
    time,
    type: 'report'
  })
  console.log(res.data)
}
//修改用户
export const changeUser = async ({ username, type, password, id }) => {
  const res = await axios.post('/api/change', {
    username,
    type,
    password,
    id
  })
  console.log(res.data)
}

//上报事件
export const createEvent = async ({
  eventNumber,
  eventType,
  eventLevel,
  eventTime,
  eventAddress,
  eventCarNumber,
  eventDriver,
  evnetLngLat,
}) => {
  const res = await axios.post('/api/setEvent', {
    eventNumber,
    eventType,
    eventLevel,
    eventTime,
    eventAddress,
    eventCarNumber,
    eventDriver,
    evnetLngLat,
  })
  console.log()
  console.log(res.data)
}

//获取事件信息
export const getEvent = async () => {
  try {
    var res = await axios.get('/api/getEvent')
    console.log(res.data)
    return res.data
  } catch (err) {
    console.log(err)
  }
}
